package entity

import (
	"fmt"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

//ShAsseFyzj 绩费增减
type ShAsseFyzj struct {
	ID     int       `gorm:"AUTO_INCREMENT;primary_key;column:FYID"`
	DTID   time.Time `gorm:"column:DTID"`
	UTID   int       `gorm:"column:UTID"`
	UTNM   string    `gorm:"column:UTNM"`
	EMID   int       `gorm:"column:EMID"`
	LREJ   float64   `gorm:"column:LREJ"`
	JXEJ   float64   `gorm:"column:JXEJ"`
	EMNM   string    `gorm:"column:EMNM"`
	FYEJ   float64   `gorm:"column:FYEJ"`
	EXPL   string    `gorm:"column:EXPL"`
	PARAID int       `gorm:"column:PARAID"`
	KHCS   string    `gorm:"column:KHCS"`
	JSJF   string    `gorm:"column:JSJF"`
	CZRID  int       `gorm:"column:CZRID"`
	CZR    string    `gorm:"column:CZR"`
	CZRQ   time.Time `gorm:"column:CZRQ"`
}

//TableName  绩费增减表名
func (ShAsseFyzj) TableName() string {
	return "SH_ASSE_FYZJ"
}

// BeforeSave
func (s *ShAsseFyzj) BeforeSave(tx *gorm.DB) (err error) {
	s.CZRQ = time.Now()
	if s.EMID == 0 {
		err = tx.Raw(`SELECT "EMID" FROM "V_USER_DEPT" WHERE "EMNM"=? AND "DPNM"=?`, s.EMNM, s.UTNM).Row().Scan(&s.EMID)
		if err != nil || s.EMID == 0 {
			err = fmt.Errorf("在部门%s中,员工%s不存在！", s.UTNM, s.EMNM)
			return
		}
	}
	if s.JSJF == "是" || s.JSJF == "1" {
		s.JSJF = "1"
	} else {
		s.JSJF = "0"
	}
	if s.JSJF == "1" {
		if s.PARAID == 0 {
			err = tx.Raw(`SELECT "ID" FROM "SH_ASSE_PARAM" WHERE "NAME"=?`, s.KHCS).Row().Scan(&s.PARAID)
			if err != nil || s.PARAID == 0 {
				return fmt.Errorf("业务类型%s在系统中不存在", s.KHCS)
			}
		}
		err = tx.Raw(`SELECT "NAME" FROM "SH_ASSE_PARAM" WHERE "ID"=?`, s.PARAID).Row().Scan(&s.KHCS)
		if err != nil {
			return
		}
		err = tx.Raw(`SELECT "EMNM","DPID","DPNM" FROM "V_USER_DEPT" WHERE "EMID"=?`, s.EMID).Row().Scan(&s.EMNM, &s.UTID, &s.UTNM)
		if err != nil {
			return
		}
		sql := `SELECT "JXID","FYID" FROM "SH_ASSE_PARAM" WHERE "ID"=?`
		row := tx.Raw(sql, s.PARAID).Row()
		var jxid, fyid int
		err := row.Scan(&jxid, &fyid)
		if err != nil {
			return err
		}
		var jxxs, fyxs float64
		ifNullEx := "IFNULL"
		if base.AppConf.Database.Mysql == "" {
			ifNullEx = "coalesce"
		}
		sql = fmt.Sprintf(`SELECT
		%s((
			SELECT
				A."PAVL" * B."CAVL"
			FROM
				"T_BASE_PARA" A
				INNER JOIN "T_CONF_PARA" B ON A."PANO" = B."PANO"
			WHERE
				A."PANO" = 202101
				AND A."BGDT" <= ?
			ORDER BY
				"BGDT" DESC
				LIMIT 1
				),
		0)+%s((
			SELECT
				A."PAVL" * B."CAVL"
			FROM
				"T_BASE_PARA" A
				INNER JOIN "T_CONF_PARA" B ON A."PANO" = B."PANO"
			WHERE
				A."PANO" = ?
				AND A."BGDT" <= ?
			ORDER BY
				"BGDT" DESC
				LIMIT 1
				),
		0)`, ifNullEx, ifNullEx)
		row = tx.Raw(sql, s.DTID, jxid, s.DTID).Row()
		err = row.Scan(&jxxs)
		if err != nil {
			return err
		}
		sql = fmt.Sprintf(`SELECT
		%s((
			SELECT
				A."PAVL" * B."CAVL"
			FROM
				"T_BASE_PARA" A
				INNER JOIN "T_CONF_PARA" B ON A."PANO" = B."PANO"
			WHERE
				A."PANO" = 202102
				AND A."BGDT" <= ?
			ORDER BY
				"BGDT" DESC
				LIMIT 1
				),
		0)+%s((
			SELECT
				A."PAVL" * B."CAVL"
			FROM
				"T_BASE_PARA" A
				INNER JOIN "T_CONF_PARA" B ON A."PANO" = B."PANO"
			WHERE
				A."PANO" = ?
				AND A."BGDT" <= ?
			ORDER BY
				"BGDT" DESC
				LIMIT 1
				),
		0)`, ifNullEx, ifNullEx)
		row = tx.Raw(sql, s.DTID, fyid, s.DTID).Row()
		err = row.Scan(&fyxs)
		if err != nil {
			return err
		}
		jxej := s.LREJ * jxxs
		fyej := s.LREJ * fyxs
		s.JXEJ = jxej
		s.FYEJ = fyej
	} else {
		tx.Raw(`SELECT "EMNM","DPID","DPNM" FROM "V_USER_DEPT" WHERE "EMID"=?`, s.EMID).Row().Scan(&s.EMNM, &s.UTID, &s.UTNM)
	}
	return
}

//ShAsseFyzjEF 返回结构体
func ShAsseFyzjEF() base.EntityFunc {
	return func() interface{} {
		entity := ShAsseFyzj{}
		return &entity
	}
}

//ShAsseFyzjESF 返回结构体数组
func ShAsseFyzjESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ShAsseFyzj
		return &entities
	}
}

func init() {
	dao := base.NewDefaultDao(ShAsseFyzjEF(), ShAsseFyzjESF(), nil, "my")
	filter := base.DefaultFilter{
		FilterColName: "EMID",
	}
	filter.SetDB("my")
	dao.Fileter = &filter
	base.DaoPoolObj.Put("ShAsseFyzj", dao)
}
